import streamlit as st
import plotly.express as px


def render_data_analysis(df):
    st.header("📊 招聘市场数据看板")

    # ========== 侧边栏过滤器 ==========
    with st.sidebar:
        st.subheader("数据过滤器")
        selected_cities = st.multiselect(
            "选择城市",
            options=df['city'].unique(),
            default=["北京", "上海", "深圳"]
        )
        # 使用 avg_salary 列设置范围
        salary_range = st.slider(
            "薪资范围 (k)",
            min_value=float(df['avg_salary'].min()),
            max_value=float(df['avg_salary'].max()),
            value=(10.0, 30.0)
        )

    # ========== 数据展示 ==========
    filtered_df = df[
        (df['city'].isin(selected_cities)) &
        (df['avg_salary'].between(salary_range[0], salary_range[1]))  # 使用 avg_salary 过滤
    ]

    # 布局三列
    col1, col2, col3 = st.columns(3)

    with col1:
        st.subheader("城市岗位分布")
        city_counts = filtered_df['city'].value_counts().reset_index()
        fig = px.bar(city_counts, x='city', y='count')
        st.plotly_chart(fig, use_container_width=True)

    with col2:
        st.subheader("学历要求分布")
        edu_counts = filtered_df['education'].value_counts()
        fig = px.pie(edu_counts, names=edu_counts.index, values=edu_counts.values)
        st.plotly_chart(fig, use_container_width=True)

    with col3:
        st.subheader("经验要求分析")
        exp_counts = filtered_df['experience'].value_counts()
        fig = px.line(exp_counts, x=exp_counts.index, y=exp_counts.values)
        st.plotly_chart(fig, use_container_width=True)

    # 详细数据表格
    st.subheader("岗位详情数据")
    st.dataframe(filtered_df[['position', 'city', 'salary', 'company']], height=300)